package com.sunny.ynhz.ui.ynts.partorlstaticfragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sunny.ynhz.R;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.ui.ynts.partorlstaticfragment.CityStaticFragment;
import com.sunny.ynhz.ui.ynts.partorlstaticfragment.CunStaticFragment;
import com.sunny.ynhz.ui.ynts.partorlstaticfragment.TownsStaticFragment;
import com.sunny.ynhz.utils.SPUtil;
import com.sunny.ynhz.utils.SwitchFragmentUtils;

import butterknife.BindView;

/**
 * Created by huhuan123 on 2018/8/7.
 *
 * @describe TODO
 */

public class RiverStaticActivity extends AppBaseTranslationTitleBarActivity {
    @BindView(R.id.rg_static)
    RadioGroup radioGroup;
    @BindView(R.id.rb_city)
    RadioButton rb_city;
    @BindView(R.id.rb_street)
    RadioButton rb_street;
    @BindView(R.id.rb_cun)
    RadioButton rb_cun;
    @BindView(R.id.ll_line)
    LinearLayout ll_line;
    private TownsStaticFragment instanceTownsStaticFragment;
    private CityStaticFragment instanceCityStaticFragment;
    private CunStaticFragment instanceCunStaticFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_river_static);
        setTitle("巡河达标");

        instanceTownsStaticFragment = TownsStaticFragment.getInstanceTownsStaticFragment();
        instanceCityStaticFragment = CityStaticFragment.getInstanceCityStaticFragment();
        instanceCunStaticFragment = CunStaticFragment.getInstanceCunStaticFragment();
        SwitchFragmentUtils switchFragmentUtils = new SwitchFragmentUtils(this, getSupportFragmentManager());
        int userType = Integer.parseInt(SPUtil.get("userType", "").toString());
        if (userType <= 15 && userType > 11) {
            switchFragmentUtils.swithFragment(instanceTownsStaticFragment);
            rb_street.setVisibility(View.VISIBLE);
            rb_city.setVisibility(View.GONE);
            rb_street.setChecked(true);
            ll_line.removeViewAt(0);
            selectView(0);

        } else if (userType == 16) {
            switchFragmentUtils.swithFragment(instanceCunStaticFragment);
            rb_cun.setChecked(true);
            rb_city.setVisibility(View.GONE);
            rb_street.setVisibility(View.GONE);
            ll_line.removeViewAt(0);
            ll_line.removeViewAt(1);
            selectView(0);

        } else {
            switchFragmentUtils.swithFragment(instanceCityStaticFragment);
            rb_city.setChecked(true);
            selectView(0);
        }
//        switchFragmentUtils.swithFragment(instanceCityStaticFragment);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rb_street) {
                    switchFragmentUtils.swithFragment(instanceTownsStaticFragment);
                    if (ll_line.getChildCount() == 2) {
                        selectView(0);
                    } else {
                        selectView(1);
                    }

                } else if (i == R.id.rb_city) {
                    switchFragmentUtils.swithFragment(instanceCityStaticFragment);
                    selectView(0);
                } else {
                    switchFragmentUtils.swithFragment(instanceCunStaticFragment);
                    if (ll_line.getChildCount() == 1) {
                        selectView(0);
                    } else if (ll_line.getChildCount() == 2) {
                        selectView(1);
                    } else {
                        selectView(2);
                    }

                }
            }
        });

    }

    public void selectView(int position) {
        for (int i = 0; i <ll_line.getChildCount(); i++) {
            View childAt = ll_line.getChildAt(i);
            if (i == position) {
                childAt.setVisibility(View.VISIBLE);
                childAt.setBackgroundColor(Color.parseColor("#3385FF"));
            } else {
                childAt.setVisibility(View.INVISIBLE);
            }
        }

    }


}
